package com.origin.ebuy.bargd.dao.good.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.origin.ebuy.bargd.dao.good.IGoodDao;
import com.origin.ebuy.dao.comm.BaseDao;
import com.origin.ebuy.dao.comm.ResultHandler;

public class GoodDaoImpl extends BaseDao implements IGoodDao {

	/**
	 * 添加商品
	 */
	@Override
	public int addGood(JSONObject object) throws SQLException {
		String[] cols = {"name_gd","price_gd","number_gd","imge_gd"};
		
		Object[] parames = {object.getString("name"),
							object.getString("price"),
							object.getString("number"),
							object.getString("image")
							};
		return insert("goods", cols, parames);
	}

	@Override
	public JSONArray queryGood(final String[] cols, String where,Object[] parames) throws SQLException {
		ResultHandler handler = new ResultHandler() {
			@Override
			public JSONArray handler(ResultSet res) throws SQLException {
				JSONArray array = new JSONArray();
				while(res.next()){
					JSONObject object = new JSONObject();
					for(String col:cols){
						object.put(col,res.getString(col));
					}
					array.add(object);
				}
				return array;
			}
		};
		return (JSONArray)query("goods", cols, where, parames, handler);
	}

	/**
	 * 修改商品
	 */
	@Override
	public int updateGood(String[] cols, String where, Object[] param)
			throws SQLException {
		return update("goods", cols, where, param);
	}

	/**
	 * 删除商品
	 */
	@Override
	public int deleteById(String id) throws SQLException {
		String[] cols = {"state"};
		Object[]  parames = {"0",id};
		return update("goods", cols, "id_gd=?", parames);
	}

}
